package com.farm.monitor;

// 具体主题 - 环境传感器类
import java.util.ArrayList;
import java.util.List;

public class EnvironmentSensor implements SensorSubject {
    private List<SensorObserver> observers = new ArrayList<>();
    private double temperature;
    private double humidity;
    private double pressure;

    // 传感器数据更新
    public void setMeasurements(double temperature, double humidity, double pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }

    private void measurementsChanged() {
        notifyObservers();
    }

    @Override
    public void registerObserver(SensorObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(SensorObserver observer) {
        observers.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (SensorObserver observer : observers) {
            observer.update(this);
        }
    }

    public double getTemperature() {
        return temperature;
    }

    public double getHumidity() {
        return humidity;
    }

    public double getPressure() {
        return pressure;
    }
}